﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class member_cert : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }        
        if (!UserPower.Contains("Manager"))
        {
            Response.Write("你没有权限！");
            Response.End();
        }
        //this.EnableViewState = false;
        string id = Request.Params["SID"];
        Response.Write(Init.InitDefault());
        DataTable dt = mssql.ExecuteDt("select dictlist as leibie from gldict(nolock) where fdname='leibie' order by xlh");
        if (!IsPostBack)
        {            
            ddlleibie.DataSource = dt;
            ddlleibie.DataTextField = ddlleibie.DataValueField = "leibie";
            ddlleibie.DataBind();
            ddlleibie.SelectedValue = leibie;
            ddlleibie.Items.Add(new ListItem("全部"));
            ddlleibie.Items.FindByValue("全部").Selected = true;
        }
        
         
            DataSet ds = DataGroup(id);
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "spid" };//主键
            GridView1.DataBind();
    }

    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public string leibie = "";

    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = DataGroup();
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "spid" };//主键
        int recordCount = GridView1.Rows.Count;
        //list(spinfo.Text, pihao.Text);
        GridView1.DataBind();
    }

    //展示
    public DataSet DataGroup(string SID="")
    {
        string keyword = k.Text;
        string shengccj = cj.Text;
        string isup = f.Text;
         leibie = ddlleibie.SelectedValue;
         leibie = ddlleibie.SelectedItem.Text;
        string sql = "select top 2000 spid,spbh,spmch,shpgg,dw,shengccj,pizhwh,bh,'' as pj "
            + " from spkfk(nolock) where  beactive='是'";
            sql += (!string.IsNullOrEmpty(keyword)) ? " and (spbh like '%" + keyword + "%' or spmch like '%" + keyword + "%' or zjm like '%" + keyword + "%') " : "";
            sql += (!string.IsNullOrEmpty(shengccj)) ? " and shengccj like '%" + shengccj + "%' " : "";
            sql += (leibie!="全部") ? " and leibie = '" + leibie + "' " : "";
            sql += (!string.IsNullOrEmpty(SID)) ? " and spid= '" + SID + "' " : "";
            sql += (isup == "1") ? " and spid not in(select detailid from [ImageDoc](nolock) where type_id ='pj' group by detailid) " : "";
            sql += (isup == "2") ? " and spid in(select detailid from [ImageDoc](nolock) where type_id ='pj' group by detailid) " : "";
            sql += " order by spid desc";
            if (Init.IsDebug)
            {
                ResultDeBug.Text = sql;
            }
        DataSet dss = mssql.ExecuteDs(sql);
        for (int i = 0; i < dss.Tables[0].Rows.Count; i++)
        {
            dss.Tables[0].Rows[i]["pj"] = herf(dss.Tables[0].Rows[i]["spid"].ToString(), "pj");
        }
        return dss;
    }

    public string herf(string id, string ret)
    {
        DataSet dsmx = new DataSet();
        dsmx.Clear();
        string detailid = id;
        string retsult = "";
        string sql = "select rtrim(imageid) as imageid,rtrim(url) as url from [ImageDoc](nolock) where type_id=rtrim('" + ret + "') and detailid=rtrim('" + detailid + "')";
        dsmx = mssql.ExecuteDs(sql);
        if (dsmx.Tables[0].Rows.Count == 0)
        {
            retsult = retsult + " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" style=\"color: #666; border: 1px solid #d2ccc4;display: inline-block;	margin: 5px 0;\">上传</a> ";
        }
        else
        {

            retsult = retsult + " <a class=\"upload\" href=\"javascript:;\" data-type=\"file\" data-dap=\"" + ret + "\" data-id=\"" + id + "\" style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;	margin: 5px 0;\">上传</a> ";
            int rowid=0;
			for (int i = 0; i < dsmx.Tables[0].Rows.Count; i++)
            {
                string fileurl = Init.imagefileurl(dsmx.Tables[0].Rows[i]["url"].ToString());
                if (!string.IsNullOrEmpty(fileurl))
                {
                    retsult += " <a class=\"btn-view preview\" href=\"" + fileurl + "\" rel=\"" + dsmx.Tables[0].Rows[i]["rel"].ToString() + "\" " + (rowid == 0 ?"": " style=\"display:none;\"") + ">预览</a>\r\n";
					rowid++;
                }
                /*
                if (i == 0)
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                else
                {
                    retsult = retsult + " <a href=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\"  data-img=\"" + dsmx.Tables[0].Rows[i]["imageid"].ToString() + "\" rel=\"" + id + "\"  src=\"" + dsmx.Tables[0].Rows[i]["url"].ToString() + "\" data-dap=\"" + ret + "\"  class=\"view\" style=\"display:none;\"  style=\"color: #ea394c;border: 1px solid #d2ccc4;display: inline-block;margin: 5px 0;\">预览</a> ";
                }
                 */
            }
        }
        return retsult;
    }
    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        DataSet ds = DataGroup();
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "spid" };//主键
        GridView1.DataBind();
    }
}